from soui4py import *

class SLvAdapterBase(SLvAdapter):
    def __init__(self):
        super().__init__()
        self.datas = []

    def AppendItem(self, item, update=False):
        self.datas.append(item)
        return len(self.datas)
    
    def InsertItem(self, position, data, update=False):
        self.datas.insert(position, data)
        if update:
            self.Update()
        return position
    
    def DeleteItem(self, position, update=True):
        if position < 0 or position >= len(self.datas):
            return False
        self.datas.pop(position)
        if update:
            self.Update()
        return True
    
    def GetItemData(self, position):
        if position < 0 or position >= len(self.datas):
            return None
        return self.datas[position]
    
    def Index(self, item):
        try:
            return self.datas.index(item)
        except: return -1
    
    def DeleteAll(self):
        self.datas.clear()
    
    def Sort(self, key=None, reverse=False, update=False):
        self.datas = sorted(self.datas, key=key, reverse=reverse)
        if update: self.Update()
    
    def SubscribeEvent(self, wnd, event_id, func):
        slot = SOUI.SOUI_FACTORY.CreateFuncSlot(func)
        wnd.SubscribeEvent(event_id, slot)
        slot.Release()
    
    def Items(self):
        return self.datas
    
    def Update(self, invalidata=False):
        if invalidata:
            self.notifyInvalidated()
        else:
            self.notifyChanged()

    def UpdateItem(self, position):
        self.notifyItemChanged(position)
        
    def getCount(self):
        return len(self.datas)

class SMcAdapterBase(SMcAdapter):
    def __init__(self):
        super().__init__()
        self.datas = []
    
    def BindDatas(self, datas):
        self.datas = datas

    def AppendItem(self, item, update=False):
        self.datas.append(item)
        if update:
            self.Update()
        return len(self.datas) - 1
    
    def InsertItem(self, position, data, update=False):
        self.datas.insert(position, data)
        if update:
            self.Update()
        return position
    
    def DeleteItem(self, position, update=True):
        if position < 0 or position >= len(self.datas):
            return False
        self.datas.pop(position)
        if update:
            self.Update()
        return True
    
    def GetItemData(self, position):
        if position < 0 or position >= len(self.datas):
            return None
        return self.datas[position]
    
    def Index(self, item):
        try:
            return self.datas.index(item)
        except: return -1
    
    def DeleteAll(self):
        self.datas.clear()
    
    def Sort(self, key=None, reverse=False, update=False):
        self.datas = sorted(self.datas, key=key, reverse=reverse)
        if update:
            self.Update()
    
    def SubscribeEvent(self, wnd, event_id, func):
        slot = SOUI.SOUI_FACTORY.CreateFuncSlot(func)
        wnd.SubscribeEvent(event_id, slot)
        slot.Release()
    
    def Items(self):
        return self.datas
    
    def Update(self, invalidata=False):
        if invalidata:
            self.notifyInvalidated()
        else:
            self.notifyChanged()

    def UpdateItem(self, position):
        self.notifyItemChanged(position)
        
    def getCount(self):
        return len(self.datas)
